package com.example.service.impl;

import com.alibaba.fastjson.JSON;
import com.example.dao.MyProducerMapper;
import com.example.model.MyProducer;
import com.example.model.Order;
import com.example.service.MsgService;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.RequestCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.UUID;

import static com.sun.xml.internal.ws.spi.db.BindingContextFactory.LOGGER;

@Service("msgService")
public class MsgServiceImpl implements MsgService {

    @Qualifier("createProducerConfig")
    @Autowired
    DefaultMQProducer producer;

    MyProducerMapper myProducerMapper;

    @Resource
    public void setMyProducerMapper(MyProducerMapper myProducerMapper) {
        this.myProducerMapper = myProducerMapper;
    }

    @Override
    public void orderMsgSend(Integer showNum, String topic) {
        String msg = "消息共计销售" + showNum + "件";
        Message sendMsg = new Message(topic,"DemoTag",msg.getBytes());
        sendMsg.setKeys(System.currentTimeMillis() + "");
        //默认3秒超时
        SendResult sendResult = null;
        try {
//            // 同步发送
//            sendResult = producer.send(sendMsg);
            // 异步发送 消费者
            producer.request(sendMsg, new RequestCallback() {
                @Override
                public void onSuccess(Message message) {
                   System.out.printf("异步消息发送成功:%s", msg);
                    MyProducer myProducer = new MyProducer();
                    myProducer.setId(message.getProperties().get("UNIQ_KEY"));
                    myProducer.setMsgTopic(sendMsg.getTopic());
                    myProducer.setMegKey(sendMsg.getKeys());
                    myProducer.setMsgBody(msg);
                    // 0 - 未处理
                    myProducer.setProduceStatus(0);
                    myProducer.setMsgTag(sendMsg.getTags());
                    myProducer.setCreateTime(new Date());
                    myProducerMapper.insert(myProducer);

//                    LOGGER.info("消息发送响应信息："+sendResult.toString());
                }

                @Override
                public void onException(Throwable e) {
                    e.printStackTrace();
                }
            }, 5000l);

        } catch (Exception e) {
            e.printStackTrace();
        }



    }

    @Override
    public void userMsgSend(Order order, String topic) {
        Message sendMsg = new Message(topic, JSON.toJSONString(order).getBytes());
        sendMsg.setKeys(System.currentTimeMillis() + "");
        //默认3秒超时
        SendResult sendResult = null;
        try {
            sendResult = producer.send(sendMsg);
            MyProducer myProducer = new MyProducer();
            myProducer.setId(UUID.randomUUID().toString().replace("-", ""));
            myProducer.setMsgTopic(sendMsg.getTopic());
            myProducer.setId(sendResult.getMsgId());
            myProducer.setMegKey(sendMsg.getKeys());
            myProducer.setMsgBody(order.toString());
            // 0 - 未处理
            myProducer.setProduceStatus(0);
            myProducer.setMsgTag(sendMsg.getTags());
            myProducer.setCreateTime(new Date());
            myProducerMapper.insert(myProducer);
        } catch (Exception e) {
            e.printStackTrace();
        }
        LOGGER.info("消息发送响应信息："+sendResult.toString());
    }

}
